/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jadum.ejc.endereco.dao;

import com.jadum.ejc.model.Endereco;
import java.util.List;
import org.hibernate.SessionFactory;

/**
 *
 * @author Jadum
 */
public class EnderecoDao implements IEnderecoDao {

    private SessionFactory sessionFactory;

    @Override
    public void adicionarEndereco(Endereco endereco) {
        
        getSessionFactory().getCurrentSession().save(endereco);
        
    }

    
    @Override
    public void atualizarEndereco(Endereco endereco) {
        
        getSessionFactory().getCurrentSession().update(endereco);
        
    }

    
    @Override
    public void excluirEndereco(Endereco endereco) {
    
        getSessionFactory().getCurrentSession().delete(endereco);
        
    }

    
    @Override
    public Endereco obterEnderecoPorId(int id) {

        StringBuilder query = new StringBuilder();
        query.append("from ").append(Constantes.NOME_TABELA_ENDERECO);
        query.append(" where ").append(Constantes.ENDERECO_ID_ENDERECO);
        query.append(" = ?");

        return (Endereco) getSessionFactory().getCurrentSession()
                .createQuery(query.toString()).setParameter(0, id)
                .uniqueResult();
        
    }

    
    @Override
    public List<Endereco> obterEnderecos() {
        
        StringBuilder query = new StringBuilder();
        query.append("from ").append(Constantes.NOME_TABELA_ENDERECO);

        return getSessionFactory().getCurrentSession()
                .createQuery(query.toString()).list();
        
    }

    
    /**
     * @return the sessionFactory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
